package scu.maqiang.numeric;

class Node {
    public int iData;
    public double dData;
    public Node leftChild;
    public Node rightChild;
    public void displayNode() {
        System.out.print("{");
        System.out.print(iData);
        System.out.print(", ");
        System.out.print(dData);
        System.out.print("} ");
    }
}
public class Tree {
    private Node root;
    public Tree() {
        root = null;
    }

    public Node find(int key) {
        Node current = root;
        while(current.iData != key) {
            if (key < current.iData) {
                current = current.leftChild;
            }
            else {
                current = current.rightChild;
            }
            if(current == null) {
                return null;
            }
        }
        return current;
    }
    public void insert(int id, double dd) {
        Node newNode = new Node();
        newNode.iData = id;
        newNode.dData = dd;
        if (root == null) {
            root = newNode;
        } else {
            Node current = root;
            Node parent;
            while(true) {
                parent = current;
                if (id < current.iData) {
                    current = current.leftChild;
                    if (current == null) {
                        parent.leftChild = newNode;
                        return;
                    } else {
                        current = current.leftChild;
                        if (current == null) {
                            parent.rightChild = newNode;
                        return;
                        }
                    }
                }
            }
        }
    }
    private void inOrder(Node localRoot) {
        if (localRoot != null) {
            inOrder(localRoot.leftChild);
            System.out.println(localRoot.iData + "  ");
            inOrder(localRoot.rightChild);
        }
    }

    public Node minnum() {
        Node current, last;
        current = root;
        last = current;
        while(current != null) {
            last = current;
            current = current.leftChild;
        }
        return last;
    }

    public Node Maxinum() {
        Node current, last;
        current = root;
        last = current;
        while(current != null) {
            last = current;
            current = current.rightChild;
        }
        return last;
    }
}
